﻿using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.IO;
using System.Text;
using System.ComponentModel;
using System.Collections.Specialized;
using TA.JSON;
using System.Diagnostics;
using TA.Win.Utils.Design;
using System.Drawing.Design;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Base control for all value controls - controls that submit a value to the server.
	/// </summary>
	/// <typeparam name="T">Type of value used by this control (eg., string for TextBox).</typeparam>
	[ValidationProperty("Value")]
	public abstract class BaseValueControl<T> : BaseValueControl
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		protected BaseValueControl()
		{
		}
		#endregion
		#region PROPS
		/// <summary>
		/// Non-typed wrapper for <see cref="Value"/> property.
		/// </summary>
		public override object GenericValue
		{
			get
			{
				return Value;
			}
			set
			{
				Value = (T)value;
			}
		}
		/// <summary>
		/// The value of this control.
		/// </summary>
		[Category("Data")]
		public virtual T Value { get; set; }
		#endregion
		#region EVENTS
		#endregion
		#endregion
		#region INTERNAL
		#region VARS
		T _valueForReset;
		#endregion
		#region METHODS
		/// <summary>
		/// Saves the value for subsequent resetting by the Page's reusing logic.
		/// </summary>
		protected override void SaveValueForReset()
		{
			if (this.Value is ICloneable)
			{
				this._valueForReset = (T)((ICloneable)this.Value).Clone();
			}
			else
				this._valueForReset = this.Value;
		}
		/// <summary>
		/// Resets the value to the pre-saved state as part of the Page reusing logic.
		/// </summary>
		protected override void ResetValue()
		{
			if (this._valueForReset is ICloneable)
			{
				this.Value = (T)((ICloneable)this._valueForReset).Clone();
			}
			else
				this.Value = this._valueForReset;
		}
		#region EVENTS
		/// <summary>
		/// Raises events.
		/// </summary>
		/// <param name="name"></param>
		/// <param name="argument"></param>
		public override void RaiseEvent(string name, string argument)
		{
			switch (name)
			{
				case "ValueChanged":
					OnValueChanged(EventArgs.Empty);
					break;
			}
			base.RaiseEvent(name, argument);
		}
		/// <summary>
		/// Raises the ValueChanged event.
		/// </summary>
		/// <param name="e"></param>
		protected virtual void OnValueChanged(EventArgs e)
		{
			ValueChanged.Raise(this, e);
		}
		#endregion
		#endregion
		#endregion
	}
}